import scour from 'scourjs';
import * as ActionTypes from '../constants/ActionTypes';
import { changeJsonFileTags, changeJsonFileMarkArr, editJsonFileMarkArr  }  from '../node/node';
// PICKET_POINT

const initialState = {
  'folderTree': [],
  'jsonTree': {},
  'filterTag': [],
  'markItem': [],     //[listIndex, itemIndex]
  'pickEvent': '',
};

export default function main(state = initialState, action) {
  switch (action.type) {
  case ActionTypes.ADD_FILE_TREE: {
    const newTree = {'folderTree': [...state.folderTree, ...action.payload]};
    return Object.assign({}, state, newTree);
  }
  case ActionTypes.ADD_CHILDREN_TREE: {
    const data = action.payload.data;
    const pathIndexArr = action.payload.key.toString().split('-');
    let newTree;
    if (pathIndexArr.length === 1) {
      newTree = scour(state).set(['folderTree', pathIndexArr[0].toString(), 'children'], data).value;
    } else {
      const pathArr = [];
      pathArr.push('folderTree');
      pathArr.push(`${pathIndexArr[0]}`)
      for (let i = 1, len = pathIndexArr.length; i < len; i++) {
        pathArr.push('children');
        pathArr.push(`${pathIndexArr[i]}`);
      }
      pathArr.push('children');
      newTree = scour(state).set(pathArr, data).value;
    }
    return Object.assign({}, state, newTree);
  }
  case ActionTypes.ADD_JSON_FILE: {
    const newJsonTree = Object.assign({}, state.jsonTree, action.payload);
    return Object.assign({}, state, { 'jsonTree': newJsonTree });
  }
  case ActionTypes.EDIT_TAG: {
    const obj = {};
    const newObj = Object.assign({}, state.jsonTree[action.payload.path]);
    newObj.tags = action.payload.tags;
    obj[action.payload.path] = newObj;
    const newJsonTree = Object.assign({}, state.jsonTree, obj);
    changeJsonFileTags(action.payload.path, action.payload.tags);
    return Object.assign({}, state, { 'jsonTree': newJsonTree });
  }
  case ActionTypes.DO_FILTER: {
    return Object.assign({}, state, { 'filterTag': action.payload });
  }
  case ActionTypes.PICKET_POINT: {
    const oldPointArr = state.jsonTree[action.payload.path].event;
    if (action.payload.data[1] === 0) {
      oldPointArr[action.payload.data[0]].timeStart = '';
    } else if (action.payload.data[1] === 1) {
      oldPointArr[action.payload.data[0]].timeEnd = '';
    }
    const newObj = {};
    newObj[action.payload.path] = state.jsonTree[action.payload.path];
    newObj[action.payload.path].event = oldPointArr;
    const newJsonTree = Object.assign({}, state.jsonTree, newObj);
    return Object.assign({}, state, { 'pickEvent': action.payload.eventName, 'markItem': action.payload.data, 'jsonTree': newJsonTree });
  }
  case ActionTypes.CHANGE_INPUT: {
    const newObj = {};
    let markItem = [];
    if (state.jsonTree[action.payload.path]) {
      newObj[action.payload.path] = Object.assign({}, state.jsonTree[action.payload.path]);
    } else {
      newObj[action.payload.path] = {
        "path": action.payload.path,
        "tags": [],
        "event": action.payload.arr,
      }
    }
    newObj[action.payload.path].event = action.payload.arr;
    newObj[action.payload.path].event.map((item, index) => {
      return (
        item.key = index
      )
    });
    const newJsonTree = Object.assign({}, state.jsonTree, newObj);
    changeJsonFileMarkArr(action.payload.path, action.payload.arr);
    return Object.assign({}, state, { 'jsonTree': newJsonTree, 'markItem': markItem });
  }
  case ActionTypes.INSERT_MARK_POINT: {
    const newObj = {};
    newObj[action.payload.path] = state.jsonTree[action.payload.path];
    if (state.markItem[1] === 0) {
      newObj[action.payload.path].event[state.markItem[0]].timeStart = action.payload.data;
    } else if (state.markItem[1] === 1) {
      newObj[action.payload.path].event[state.markItem[0]].timeEnd = action.payload.data;
    }
    const newJsonTree = Object.assign({}, state.jsonTree, newObj);
    editJsonFileMarkArr(action.payload.path, state.markItem, newObj[action.payload.path].event);
    return Object.assign({}, state, { 'jsonTree': newJsonTree });
  }

  default:
    return state;
  }
}